// @description: 命令行处理
// @auth: bigkai
// @time: 2020/7/25

package cli

import (
	"crawl"
	"github.com/gpmgo/gopm/modules/cli"
	"os"
	"strconv"
	"strings"
)

func Start()  {
	app := &cli.App{
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name: "ns",
				Value: "0",
				Usage: "基金编号，多个参数用,隔开",
			},
			&cli.StringFlag{
				Name: "es",
				Value: "0",
				Usage: "反跑错误码，多个参数用,隔开",
			},
		},
		Action: func(ctx *cli.Context) {
			var numbers = make([]int, 0)
			var errCodes = make([]int, 0)
			numbersStr := strings.TrimSpace(ctx.String("ns"))
			errCodeStr := strings.TrimSpace(ctx.String("es"))

			if len(numbersStr) > 0 {
				numbers = stringToIntArray(numbersStr, ",")
			}

			if len(errCodeStr) > 0 {
				errCodes = stringToIntArray(errCodeStr, ",")
			}

			crawl.CrwalData(numbers, errCodes)

		},
	}

	app.Run(os.Args)
}

func stringToIntArray(str, sp string) ([]int) {
	var numbers = make([]int, 0)
	split := strings.Split(str, sp)
	for _, val := range split {
		number, err := strconv.Atoi(val)
		if err != nil {
			panic(err)
		}
		numbers = append(numbers, number)
	}
	return numbers
}